package ttestExecuter;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import ttestClosed.TestDelayQueue;
import ttestClosed.TestFrame;

public class TestExecuter {
	
	private BlockingQueue<Runnable> blockingQueue = new PriorityBlockingQueue<Runnable>();

	public static void main(String[] args) {
		TestExecuter te = new TestExecuter();
		
		testExecuter(te);
		testSchExecuter();
		
		SlotExecuter sExec = new SlotExecuter();
		new CostumThreadFactory().newThread(sExec).run();
		
		System.out.println("FINISHED!");
	}

	public static void testExecuter(TestExecuter te) {
		ThreadPoolExecutor tpe = new ThreadPoolExecutor(10, 20, 1000L, TimeUnit.MILLISECONDS, te.blockingQueue, new CostumThreadFactory());
		long delay = System.currentTimeMillis();
		try {
			tpe.awaitTermination(delay-System.currentTimeMillis(), TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		tpe.getCompletedTaskCount();
	}

	public static void testSchExecuter() {
		TestDelayQueue tdq = new TestDelayQueue();
		CostumScheduledExecuter cse = new CostumScheduledExecuter(10, new CostumThreadFactory());
		System.out.println("START!");
		cse.schedule(new TestFrame(1000, "EXECUTED!"), 1000L, TimeUnit.MILLISECONDS);
		int counter = 10;
		System.out.println(cse.getQueue().isEmpty());
		System.out.println("DELAY!");
		while (cse.getPoolSize() > 0 && counter > 0) {
//			cse.getQueue().add(new TestFrame(1000, "EXECUTED!333"));
//			cse.execute(cse.getQueue().poll());
			tdq.addFrame(new TestFrame(1000, "EXECUTED!"));
			cse.schedule(new TestFrame(1000, "EXECUTED!222"), 1000L, TimeUnit.MILLISECONDS);
			counter--;
		}
		
		cse.scheduleWithFixedDelay(new TestFrame(1000, "AHOI!"), 1000L, 100L, TimeUnit.MILLISECONDS);
		cse.shutdown();
	}
}
